<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: sap<songaiping@hrbjjhh.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;
/**
 * @todo 客服系统-活动模型
 */

class ServiceActivityModel extends BaseModel {

	/**
	 * @todo 返回主键
	 * @see \Think\Model::getPk()
	 * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
	 */
	public function getPk(){
		return 'activity_id';
	}
	
	/**
	 * @todo 活动列表拉取
	 * @author sap
	 * @since 2015-9-3
	 */
	public function activityAdd(){
		$activity_id = $this->max('`activity_id`');//取最大的id,然后取大于这个id的数据,然后写入
		$map = array();//查询条件初始化
		//数据表为空 或 最大的为0 则全部拉去，若取回的数量大于0则摘取大于最大ID的数据
		if(!is_null($activity_id) && $activity_id > 0){
			$map['activity_id'] = array('gt',$activity_id);
		}
		//活动主表数据读取
		$map['status'] = array('gt',-1);
		$activity_list= M('Activity')->where($map)->field('activity_id,activity_name,start_time,end_time')->select();
		if(empty($activity_list)){
			return true;
		}
		//写入活动派生表
		$result = $this->addAll($activity_list,array(),true);
		if(!$result){
			$this->error = '拉取失败';
			return false;
		}
		S('service_activity_list',null);//清除缓存
		return true;
	}
	
	/**
	 * @todo 活动详情（一条）
	 * @param $activity_id
	 * @author sap
	 * @since 2015-9-3
	 */
	public function activityShow($activity_id=0,$field=''){
		//获取数据
		$activity_id = (int)$activity_id;
		if($activity_id == 0){
			$this->error = '参数丢失';
			return false;
		}
		//查询
		$map = array();//查询条件初始化
		$map['activity_id'] = $activity_id;
		$map['status'] = array('gt',-1);
		return $this->where($map)->field($field)->find();
	}

	/**
	 * @todo 添加活动下的销售网点/班车点/短信模板
	 * @param number	$activity_id	活动ID
	 * @param unknown	$arr			活动下要添加的销售网点ID数组/班车点ID数组/短信模板ID数组
	 * @param unknown	$primary_key	销售网点ID/班车点ID/短信模板ID，表中字段名
	 * @param string	$model			表名
	 * @param string	$cache			清除缓存名
	 * @return boolean
	 */
	public function addActivityRelation($activity_id=0,$arr=array(),$primary_key='',$model='',$cache=''){
		$activity_id = (int)$activity_id;
		$arr = array_filter(array_unique((array)$arr));
		if($activity_id == 0 || $primary_key == '' || $model == '' || $cache == ''){
			$this->error = '参数丢失';
			return false;
		}
		if(empty($arr)){
			$this->error = '请选择要添加的数据';
			return false;
		}
		$data = array();//添加数据
		$count = count($arr);
		if($count > 0){
			for($i=0;$i<$count;$i++){
				$data[$i]['activity_id'] = $activity_id;
				$data[$i][$primary_key] = $arr[$i];
			}
		}
		if(M($model)->addAll($data,array(),true) === false){
			$this->error = '操作失败';
			return false;
		}
		S($cache,null);//清除缓存
		return true;
	}
	
	/**
	 * @todo 添加活动下的发车时间
	 */
	public function addActivityDepartureTime($activity_id,$departure_time){
		//获取数据
		$activity_id = (int)$activity_id;
		$departure_time = (int)$departure_time;
		if($activity_id == 0 || $departure_time == 0){
			$this->error = '参数丢失';
			return false;
		}
		//操作数据库
		$data = array();//写入数据初始化
		$data['activity_id'] = $activity_id;
		$data['departure_time'] = $departure_time;
		$obj = M('ServiceActivityDepartureTime');
		$info = $obj->where($data)->find();
		if(!empty($info)){
			$this->error = '该活动下该时间已经添加过';
			return false;
		}
		if($obj->add($data) === false){
			$this->error = '操作失败';
			return false;
		}
		S('service_activity_departure_time',null);//清除缓存
		return true;
	}

	/**
	 * @todo 改变活动状态
	 * @author sap
	 * @since 2015-9-3
	 */
	public function changeStatus($activity_id=0,$status=0){
		//获取数据
		if($activity_id == 0 || $status == 0){
			$this->error = '参数丢失';
			return false;
		}
		$info = $this->activityShow($activity_id,'activity_id,status');//活动是否存在
		//判断活动状态是否正确
		switch ($status){
			case '1':
				if($info['status'] != 0){
					$this->error = '请正确操作';
					return false;
				}
				break;
			case '2':
				if($info['status'] != 1){
					$this->error = '请正确操作';
					return false;
				}
				break;
			case '3':
				if($info['status'] != 2){
					$this->error = '请正确操作';
					return false;
				}
				break;
			case '4':
				if($info['status'] != 3){
					$this->error = '请正确操作';
					return false;
				}
				break;
			default:
				$this->error='参数非法';
				return false;
		}
		$map = array();$data = array();//修改条件初始化
		$map['activity_id'] = $activity_id;
		//生成回访任务时，清除该任务下报名表的通话记录
		$this->startTrans();
		if($status == 3){
			$obj = M('ServiceActivityJoin');
			$data_join = array();
// 			$data_join['is_valid'] = 0;
			$data_join['is_call'] = 0;
			$data_join['later_call_time'] = 0;
			$data_join['last_call_time'] = 0;
			if($obj->where($map)->save($data_join) === false){
				$this->rollback();
				$this->error = '生成回访任务失败，请重试';
				return false;
			}
		}
		//任务派生表修改状态
		$data['status'] = $status;
		if($this->where($map)->save($data) === false){
			$this->rollback();
			$this->error = '操作失败，请重试';
			return false;
		}
		$this->commit();
		//清除缓存
		S('service_activity_list',null);
		//记录行为
		switch ($status){
			case '1':
				action_log('change_status_one', 'service_activity', $activity_id, UID);
				break;
			case '2':
				action_log('change_status_two', 'service_activity', $activity_id, UID);
				break;
			case '3':
				action_log('change_status_three', 'service_activity', $activity_id, UID);
				break;
			case '4':
				action_log('change_status_four', 'service_activity', $activity_id, UID);
				break;
		}
		return true;
	}

	/**
	 * @todo 读取所有不在该活动下的短信模板
	 * @param int $activity_id 活动ID
	 * @author sap
	 * @since 2015-9-11
	 */
	public function notIsActivitySms($activity_id=0){
		//获取数据
		$activity_id = (int)$activity_id;
		if($activity_id == 0){
			$this->error = '参数丢失';
			return false;
		}
		
		//活动下短信模板
		$obj = D('Admin/ServiceApi');
		$activity_list = $obj->getActivitySmsTemplate($activity_id);
		
		//所有短信模板
		$list = D('Admin/ServiceSmsTemplate')->getSmsTemplateList(array('status'=>1),'sms_template_id,sms_template_name');
		if(empty($list)){
			$this->error = '暂无短信模板';
			return false;
		}
			
		$result = array();//返回值初始化
		$diff=array();//数组差
		
		//所有不在该活动下的短信模板
		if(!empty($activity_list)){
			$sms_template_id_all = array_column($list, 'sms_template_id');//全部销售网点ID
			$sms_template_id_activity = array_column($activity_list, 'sms_template_id');//活动下销售网点ID
			$diff = array_diff($sms_template_id_all, $sms_template_id_activity);//数组差
		}else{
			$result = $list;
		}
		if(!empty($diff)){
			$count = count($list);
			for($i=0;$i<$count;$i++){
				if(in_array($list[$i]['sms_template_id'],$diff)){
					//在数组中
					$result[] = $list[$i];
				}
			}
		}
		return $result;
	}
}
